<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">产品集合新建</p>
    </div>
    <div class="view-container">
      <el-row class="status-title">
        <el-col class="mr20 width100">
          <el-button
            class="btn-success width100"
            type="primary"
            @click="submitForm()"
          >确定</el-button>
        </el-col>
      </el-row>
      <el-form ref="form" :model="form" label-width="140px" class="mt30">
        <el-row>
          <el-col :span="8">
            <el-form-item label="集合名称:" prop="sortName">
              <el-input v-model="form.sortName" placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="排序:" prop="orderNum">
              <el-select v-model="form.orderNum" style="width:100%;" placeholder="请选择">
                <el-option :label="0" :value="0"></el-option>
                <el-option
                  v-for="(i, index) in 9"
                  :key="index"
                  :label="i"
                  :value="i">
                </el-option>
              </el-select>
              <!-- <el-input v-model="form.orderNum" placeholder="" /> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="添加产品:">
              <el-button
                class="btn-default width100"
                type="primary"
                @click="onClickLayer"
              >选择产品</el-button>
              <div v-for="(item, index) in ids" :key="index">
                {{ item.specificationCode + '-' + item.specificationName }}
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(item)"
                ></el-button>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 产品弹出对话框 -->
    <el-dialog title="产品" :visible.sync="open" width="60%" top="50px" append-to-body center>
      <el-row>
        <el-col :span="24">
          <el-form :model="queryParams" ref="queryForm" :inline="true" @submit.native.prevent>
            <el-form-item prop="searchValue">
              <el-input
                v-model="queryParams.searchValue"
                placeholder="名称或者编码模糊查询"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="list-search btn-success" color="#5ED4D4">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" class="list-search">重置</el-button>
            </el-form-item>
          </el-form>
          <el-table :data="searchProductList" ref="materialTable" @selection-change="handleSelectionChange" :row-key="getRowKeys">
            <el-table-column type="selection" width="35" align="center" :reserve-selection="true" />
            <el-table-column label="序号" type="index" align="center" width="50">
              <template slot-scope="scope">
                <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
              </template>
            </el-table-column>
            <el-table-column label="SKU编码" align="center" prop="specificationCode" :show-overflow-tooltip="true" width="150" />
            <el-table-column label="SKU名称" align="center" prop="specificationName" :show-overflow-tooltip="true" width="180" />
            <el-table-column label="图片" align="center" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <img v-if="scope.row.imgUrl" :src="scope.row.imgUrl" width="50" height="50" />
              </template>
            </el-table-column>
            <el-table-column label="单位" align="center" prop="unit" :show-overflow-tooltip="true" />
            <el-table-column label="审核状态" align="center" prop="approvalStatus">
              <template slot-scope="scope">
                <div v-if="scope.row.approvalStatus == 0" style="color:#13C2C2"></div>
                <div v-if="scope.row.approvalStatus == 1" style="color:#13C2C2">待审核</div>
                <div v-if="scope.row.approvalStatus == 2" style="color:#52C41A">已审核</div>
                <div v-if="scope.row.approvalStatus == 3" style="color:#fd562c">拒绝</div>
              </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="productStatus">
              <template slot-scope="scope">
                <div v-if="scope.row.productStatus == 0" style="color:#13C2C2">禁用</div>
                <div v-if="scope.row.productStatus == 1" style="color:#52C41A">正常</div>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList()"
          />
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { proCollectionAdd } from "@/api/system/basedata/productCollection/productCollection"
import { listSku } from "@/api/system/basedata/product/product"

export default {
  name: "add",
  data(){
    return {
      form: {
        sortName: '', // 集合名称
        orderNum: '', // 排序
        commodityCollectionDetailList: [], // 产品集合
      },
      open: false,
      searchProductList: [], // 产品列表
      // 查询参数
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productStatus : 1,
        approvalStatus: 2,
        searchValue: undefined
      },
      ids: [],
    }
  },
  methods: {
    // 弹窗显示
    onClickLayer(){
      this.open = true
      this.getList()
    },
    getList() {
      listSku(this.addDateRange(this.queryParams)).then(res => {
          this.searchProductList = res.rows
          this.total = res.total
      })
    },
    // 搜索按钮
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置按钮
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 翻页不清空
    getRowKeys(row){
      return row.productSpecificationsId
    },
    // 选中产品
    handleSelectionChange(row){
      this.ids = row.map(item => item)
      let arr = []
      this.ids.map(i => {
          arr.push(Object.assign(
              {},{
                productId: i.productId,
                productCode: i.productCode,
                productName: i.productName,
                specificationsId: i.productSpecificationsId,
                specificationsCode: i.specificationCode,
                specificationsName: i.specificationName,
              }
          ))
      })
      this.form.commodityCollectionDetailList = arr
    },
    // 删除产品
    handleDelete(item){
      let index = this.ids.findIndex(i => {
        return i.productSpecificationsId == item.productSpecificationsId
      })
      this.ids.splice(index, 1)
      this.form.commodityCollectionDetailList.splice(index, 1)
      this.searchProductList.find(y => {
        if(item.productSpecificationsId == y.productSpecificationsId){
          this.$refs.materialTable.toggleRowSelection(y, false)
        }
      })
    },
    // 提交数据
    submitForm() {
      // console.log(this.form)
      if(this.form.sortName == ''){
        this.$modal.msgError('请填写集合名称！')
      }else if(this.form.orderNum == ''){
        this.$modal.msgError('请选择排序！')
      }else{
        this.$modal.loading("正在提交")
        proCollectionAdd(this.form).then(res => {
          this.$tab.closeOpenPage({ path: "/basedata/product/productCollection" })
          this.$modal.msgSuccess("新建成功!")
          this.$modal.closeLoading()
        })
      }
    }
  }
}
</script>